home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / frasrc19.zip / DISKVID.C < prev    next >
C/C++ Source or Header  |  1995-02-19  |  25KB  |  770 lines

  1. /*
  2.    "Disk-Video" (and RAM-Video and Expanded-Memory Video) routines
  3.  
  4.    Reworked with fast caching July '90 by Pieter Branderhorst.
  5.    (I'm proud of this cache handler, had to get my name on it!)
  6.    Caution when modifying any code in here:  bugs are possible which
  7.    slow the cache substantially but don't cause incorrect results.
  8.    Do timing tests for a variety of situations after any change.
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #ifndef XFRACT
  14. #include <dos.h>
  15. #endif
  16. #include <string.h>
  17. #include "fractint.h"
  18. #include "prototyp.h"
  19.  
  20. #define BOXROW     6
  21. #define BOXCOL     11
  22. #define BOXWIDTH 57
  23. #define BOXDEPTH 12
  24.  
  25. int disk16bit = 0;       /* storing 16 bit values for continuous potential */
  26.  
  27. static int timetodisplay;
  28. static FILE *fp = NULL;
  29. static int disktarga;
  30.  
  31. #define BLOCKLEN 64    /* must be a power of 2, must match next */
  32. #define BLOCKSHIFT 6    /* must match above */
  33. #define CACHEMIN 4    /* minimum cache size in Kbytes */
  34. #define CACHEMAX 64    /* maximum cache size in Kbytes */
  35. #define FREEMEM  33    /* try to leave this much far memory unallocated */
  36. #define HASHSIZE 1024    /* power of 2, near CACHEMAX/(BLOCKLEN+8) */
  37.  
  38. static struct cache {    /* structure of each cache entry */
  39.    long offset;            /* pixel offset in image */
  40.    BYTE pixel[BLOCKLEN];  /* one pixel per byte (this *is* faster) */
  41.    unsigned int hashlink;       /* ptr to next cache entry with same hash */
  42.    unsigned int dirty : 1;       /* changed since read? */
  43.    unsigned int lru : 1;       /* recently used? */
  44.    } far *cache_end, far *cache_lru, far *cur_cache;
  45.  
  46. static struct cache far *cache_start = NULL;
  47. static long high_offset;       /* highwater mark of writes */
  48. static long seek_offset;       /* what we'll get next if we don't seek */
  49. static long cur_offset;        /* offset of last block referenced */
  50. static int cur_row;
  51. static long cur_row_base;
  52. static unsigned int far *hash_ptr = NULL;
  53. static int pixelshift;
  54. static int headerlength;
  55. static unsigned int rowsize = 0;   /* doubles as a disk video not ok flag */
  56. static unsigned int colsize;       /* sydots, *2 when pot16bit */
  57.  
  58. static BYTE far *charbuf = NULL;   /* currently used only with XMM */
  59.  
  60. /* For expanded memory: */
  61. static BYTE far *expmemoryvideo;
  62. static int oldexppage,expoffset;
  63. static unsigned int emmhandle = 0;
  64.  
  65. /* For extended memory: */
  66. static unsigned int xmmhandle = 0;
  67. static long extoffset;
  68. static BYTE far *extbufptr, far *endreadbuf, far *endwritebuf;
  69. #define XMMREADLEN 64    /* amount transferred from extended mem at once */
  70. #define XMMWRITELEN 256 /* max amount transferred to extended mem at once,
  71.                must be a factor of 1024 and >= XMMREADLEN */
  72.  
  73. static void _fastcall near findload_cache(long);
  74. static struct cache far * _fastcall near find_cache(long);
  75. static void near write_cache_lru(void);
  76. static void (_fastcall near *put_char)(BYTE);
  77. static void _fastcall near disk_putc(BYTE);
  78. static void _fastcall near exp_putc(BYTE);
  79. static void _fastcall near ext_putc(BYTE);
  80. static BYTE (near *get_char)(void);
  81. static BYTE near disk_getc(void);
  82. static BYTE near exp_getc(void);
  83. static BYTE near ext_getc(void);
  84. static void (_fastcall near *doseek)(long);
  85. static void _fastcall near disk_seek(long);
  86. static void _fastcall near exp_seek(long);
  87. static void _fastcall near ext_seek(long);
  88.  
  89. int made_dsktemp = 0;
  90. char diskfilename[] = {"FRACTINT.$$$"};
  91.  
  92. int startdisk()
  93. {
  94.    if (!diskisactive)
  95.       return(0);
  96.    headerlength = disktarga = 0;
  97.    return (common_startdisk(sxdots,sydots,colors));
  98.    }
  99.  
  100. int pot_startdisk()
  101. {
  102.    int i;
  103.    if (dotmode == 11) /* ditch the original disk file */
  104.       enddisk();
  105.    else
  106.    {
  107.       static FCODE msg[] = {"clearing 16bit pot work area"};
  108.       showtempmsg(msg);
  109.    }
  110.    headerlength = disktarga = 0;
  111.    i = common_startdisk(sxdots,sydots<<1,colors);
  112.    cleartempmsg();
  113.    if (i == 0)
  114.       disk16bit = 1;
  115.    return (i);
  116.    }
  117.  
  118. int targa_startdisk(FILE *targafp,int overhead)
  119. {
  120.    int i;
  121.    if (dotmode == 11) { /* ditch the original disk file, make just the targa */
  122.       enddisk();      /* close the 'screen' */
  123.       setnullvideo(); /* set readdot and writedot routines to do nothing */
  124.       }
  125.    headerlength = overhead;
  126.    fp = targafp;
  127.    disktarga = 1;
  128.    i = common_startdisk(sxdots*3,sydots,colors);
  129.    high_offset = 100000000L; /* targa not necessarily init'd to zeros */
  130.    return (i);
  131. }
  132.  
  133. int _fastcall common_startdisk(long newrowsize, long newcolsize, int colors)
  134. {
  135.    int i,success,freemem;
  136.    long memorysize;
  137.    unsigned int far *fwd_link = NULL;
  138.    struct cache far *ptr1 = NULL;
  139.    long longtmp;
  140.    unsigned int cache_size;
  141.    int exppages;
  142.    struct XMM_Move MoveStruct;
  143.    BYTE far *tempfar = NULL;
  144.    success = 0;
  145.    if (diskflag)
  146.       enddisk();
  147.    if (dotmode == 11) { /* otherwise, real screen also in use, don't hit it */
  148.       char buf[20];
  149.       helptitle();
  150.       setattr(1,0,C_DVID_BKGRD,24*80);    /* init rest to background */
  151.       for (i = 0; i < BOXDEPTH; ++i)
  152.      setattr(BOXROW+i,BOXCOL,C_DVID_LO,BOXWIDTH);  /* init box */
  153.       putstring(BOXROW+2,BOXCOL+4,C_DVID_HI,"'Disk-Video' mode");
  154.       putstring(BOXROW+4,BOXCOL+4,C_DVID_LO,"Screen resolution: ");
  155.       sprintf(buf,"%d x %d",sxdots,sydots);
  156.       putstring(-1,-1,C_DVID_LO,buf);
  157.       if (disktarga)
  158.      putstring(-1,-1,C_DVID_LO,"  24 bit Targa");
  159.       else {
  160.      putstring(-1,-1,C_DVID_LO,"  Colors: ");
  161.      sprintf(buf,"%d",colors);
  162.      putstring(-1,-1,C_DVID_LO,buf);
  163.      }
  164.       putstring(BOXROW+8,BOXCOL+4,C_DVID_LO,"Status:");
  165.       {
  166.       static FCODE o_msg[] = {"clearing the 'screen'"};
  167.       char msg[sizeof(o_msg)];
  168.       far_strcpy(msg,o_msg);
  169.       dvid_status(0,msg);
  170.       }
  171.       }
  172.    cur_offset = seek_offset = high_offset = -1;
  173.    cur_row    = -1;
  174.    if (disktarga)
  175.       pixelshift = 0;
  176.    else {
  177.       pixelshift = 3;
  178.       i = 2;
  179.       while (i < colors) {
  180.      i *= i;
  181.      --pixelshift;
  182.      }
  183.       }
  184.    timetodisplay = 1000;  /* time-to-display-status counter */
  185.  
  186.    /* allocate cache: try for the max; leave FREEMEMk free if we can get
  187.       that much or more; if we can't get that much leave 1/2 of whatever
  188.       there is free; demand a certain minimum or nogo at all */
  189.  
  190.    /* allow for LogTable */
  191.    if((LogFlag || rangeslen) && !logtable_in_extra_ok())
  192.       freemem = 33;   
  193.    else
  194.       freemem = FREEMEM;   
  195.  
  196.    for (cache_size = CACHEMAX; cache_size >= CACHEMIN; --cache_size) {
  197.       longtmp = ((int)cache_size < freemem) ? (long)cache_size << 11
  198.                        : (long)(cache_size+freemem) << 10;
  199.       if ((tempfar = farmemalloc(longtmp)) != NULL) {
  200.      farmemfree(tempfar);
  201.      break;
  202.      }
  203.       }
  204.    /* need memory left for LogTable */
  205.    if(debugflag==4200) cache_size = CACHEMIN;
  206.    longtmp = (long)cache_size << 10;
  207.    cache_start = (struct cache far *)farmemalloc(longtmp);
  208.    if (cache_size == 64)
  209.       --longtmp; /* safety for next line */
  210.    cache_end = (cache_lru = cache_start) + longtmp / sizeof(*cache_start);
  211.    hash_ptr  = (unsigned int far *)farmemalloc((long)(HASHSIZE<<1));
  212.    if (cache_start == NULL || hash_ptr == NULL) {
  213.       static FCODE msg[]={"*** insufficient free memory for cache buffers ***"};
  214.       stopmsg(0,msg);
  215.       return(-1);
  216.       }
  217.    if (dotmode == 11) {
  218.       char buf[50];
  219.       sprintf(buf,"Cache size: %dK\n\n",cache_size);
  220.       putstring(BOXROW+6,BOXCOL+4,C_DVID_LO,buf);
  221.       }
  222.  
  223.    /* preset cache to all invalid entries so we don't need free list logic */
  224.    for (i = 0; i < HASHSIZE; ++i)
  225.       hash_ptr[i] = 0xffff; /* 0xffff marks the end of a hash chain */
  226.    longtmp = 100000000L;
  227.    for (ptr1 = cache_start; ptr1 < cache_end; ++ptr1) {
  228.       ptr1->dirty = ptr1->lru = 0;
  229.       fwd_link = hash_ptr
  230.      + (((unsigned short)(longtmp+=BLOCKLEN) >> BLOCKSHIFT) & (HASHSIZE-1));
  231.       ptr1->offset = longtmp;
  232.       ptr1->hashlink = *fwd_link;
  233.       *fwd_link = (char far *)ptr1 - (char far *)cache_start;
  234.       }
  235.  
  236.    memorysize = (long)(newcolsize) * newrowsize;
  237.    if ((i = (short)memorysize & (BLOCKLEN-1)) != 0)
  238.       memorysize += BLOCKLEN - i;
  239.    memorysize >>= pixelshift;
  240.    diskflag = 1;
  241.    rowsize = (unsigned int) newrowsize;
  242.    colsize = (unsigned int) newcolsize;
  243.  
  244.    if (debugflag != 420 && debugflag != 422 /* 422=xmm test, 420=disk test */
  245.      && disktarga == 0) {
  246.       /* Try Expanded Memory */
  247.       exppages = (int)((memorysize + 16383) >> 14);
  248.       if ((expmemoryvideo = emmquery()) != NULL
  249.     && (emmhandle = emmallocate(exppages)) != 0) {
  250.      if (dotmode == 11)
  251.         putstring(BOXROW+2,BOXCOL+23,C_DVID_LO,"Using your Expanded Memory");
  252.      for (oldexppage = 0; oldexppage < exppages; oldexppage++)
  253.         emmclearpage(oldexppage, emmhandle); /* clear the "video" */
  254.      put_char = exp_putc;
  255.      get_char = (BYTE (near *)(void))exp_getc;
  256.      doseek  = exp_seek;
  257.      goto common_okend;
  258.      }
  259.       }
  260.  
  261.    if (debugflag != 420 && disktarga == 0) {
  262.       /* Try Extended Memory */
  263.       if ((charbuf = farmemalloc((long)XMMWRITELEN)) != NULL
  264.     && xmmquery() != 0
  265.     && (xmmhandle = xmmallocate((unsigned int)(longtmp = (memorysize+1023) >> 10))) != 0) {
  266.      if (dotmode == 11)
  267.         putstring(BOXROW+2,BOXCOL+23,C_DVID_LO,"Using your Extended Memory");
  268.      for (i = 0; i < XMMWRITELEN; i++)
  269.         charbuf[i] = 0;
  270.      MoveStruct.SourceHandle = 0;     /* Source is in conventional memory */
  271.      MoveStruct.SourceOffset = (unsigned long)charbuf;
  272.      MoveStruct.DestHandle     = xmmhandle;
  273.      MoveStruct.Length     = XMMWRITELEN;
  274.      MoveStruct.DestOffset     = 0;
  275.      longtmp *= (1024/XMMWRITELEN);
  276.      while (--longtmp >= 0) {
  277.         if ((success = xmmmoveextended(&MoveStruct)) == 0) break;
  278.         MoveStruct.DestOffset += XMMWRITELEN;
  279.         }
  280.      if (success) {
  281.         put_char = ext_putc;
  282.         get_char = (BYTE (near *)(void))ext_getc;
  283.         doseek  = ext_seek;
  284.         extbufptr = endreadbuf = charbuf;
  285.         endwritebuf = charbuf + XMMWRITELEN;
  286.         goto common_okend;
  287.         }
  288.      xmmdeallocate(xmmhandle);     /* Clear the memory */
  289.      xmmhandle = 0;          /* Signal same */
  290.      }
  291.       }
  292.  
  293.    if (dotmode == 11)
  294.       putstring(BOXROW+2,BOXCOL+23,C_DVID_LO,"Using your Disk Drive");
  295.    if (disktarga == 0) {
  296.       if ((fp = dir_fopen(tempdir,diskfilename,"w+b")) != NULL) {
  297.      made_dsktemp = 1;
  298. #if 1                    /* added by Michael Snyder */
  299.      memset(boxy, 0, 1024);
  300.      while (memorysize > 0)
  301.      {
  302.         static FCODE cancel[] =
  303.         "Disk Video initialization interrupted:\n";
  304.  
  305.         fwrite(boxy, (memorysize > 1024) ? 1024 : (int)memorysize, 1, fp);
  306.         memorysize -= 1024;
  307.         if (keypressed())       /* user interrupt */
  308.            if (stopmsg(2, cancel))  /* esc to cancel, else continue */
  309.            {
  310.           enddisk();
  311.           return -2;            /* -1 == failed, -2 == cancel   */
  312.            }
  313.      }
  314. #else
  315.      while (--memorysize >= 0) /* "clear the screen" (write to the disk) */
  316.         putc(0,fp);
  317. #endif
  318.      if (ferror(fp)) {
  319.         static FCODE msg[]={"*** insufficient free disk space ***"};
  320.         stopmsg(0,msg);
  321.         fclose(fp);
  322.         fp = NULL;
  323.         rowsize = 0;
  324.         return(-1);
  325.         }
  326.      fclose(fp); /* so clusters aren't lost if we crash while running */
  327.      fp = dir_fopen(tempdir,diskfilename,"r+b"); /* reopen */
  328.      }
  329.       if (fp == NULL) {
  330.          char msg[80];
  331.      static FCODE s1[]={"*** Couldn't create "};
  332.      sprintf(msg,"%Fs%s",(char far *)s1,diskfilename);
  333.      stopmsg(0,msg);
  334.      rowsize = 0;
  335.      return(-1);
  336.      }
  337.       }
  338.    put_char = disk_putc;
  339.    get_char = (BYTE (near *)(void))disk_getc;
  340.    doseek  = disk_seek;
  341.  
  342. common_okend:
  343.    if (dotmode == 11)
  344.       dvid_status(0,"");
  345.    return(0);
  346. }
  347.  
  348. void enddisk()
  349. {
  350.    if (fp != NULL) {
  351.       if (disktarga) /* flush the cache */
  352.      for (cache_lru = cache_start; cache_lru < cache_end; ++cache_lru)
  353.         if (cache_lru->dirty)
  354.            write_cache_lru();
  355.       fclose(fp);
  356.       }
  357.    if (charbuf != NULL)
  358.       farmemfree((void far *)charbuf);
  359.    if (hash_ptr != NULL)
  360.       farmemfree((void far *)hash_ptr);
  361.    if (cache_start != NULL)
  362.       farmemfree((void far *)cache_start);
  363.    if (emmhandle != 0)     /* Expanded memory video? */
  364.       emmdeallocate(emmhandle);
  365.    if (xmmhandle != 0)     /* Extended memory video? */
  366.       xmmdeallocate(xmmhandle);
  367.    diskflag = rowsize = emmhandle = xmmhandle = disk16bit = 0;
  368.    hash_ptr    = NULL;
  369.    cache_start = NULL;
  370.    charbuf     = NULL;
  371.    fp           = NULL;
  372. }
  373.  
  374. int readdisk(unsigned int col, unsigned int row)
  375. {
  376.    int col_subscr;
  377.    long offset;
  378.    char buf[41];
  379.    if (--timetodisplay < 0) {  /* time to display status? */
  380.       if (dotmode == 11) {
  381.      sprintf(buf," reading line %4d",
  382.         (row >= (unsigned int)sydots) ? row-sydots : row); /* adjust when potfile */
  383.      dvid_status(0,buf);
  384.      }
  385.       timetodisplay = 1000;
  386.       }
  387.    if (row != (unsigned int)cur_row) { /* try to avoid ghastly code generated for multiply */
  388.       if (row >= colsize) /* while we're at it avoid this test if not needed  */
  389.      return(0);
  390.       cur_row_base = (long)(cur_row = row) * rowsize;
  391.       }
  392.    if (col >= rowsize)
  393.       return(0);
  394.    offset = cur_row_base + col;
  395.    col_subscr = (short)offset & (BLOCKLEN-1); /* offset within cache entry */
  396.    if (cur_offset != (offset & (0L-BLOCKLEN))) /* same entry as last ref? */
  397.       findload_cache(offset & (0L-BLOCKLEN));
  398.    return (cur_cache->pixel[col_subscr]);
  399. }
  400.  
  401. int FromMemDisk(long offset, int size, void far *dest)
  402. {
  403.    int col_subscr =  (int)(offset & (BLOCKLEN - 1));
  404.  
  405.    if (col_subscr + size > BLOCKLEN)        /* access violates  a */
  406.       return 0;                                 /*   cache boundary   */
  407.  
  408.    if (cur_offset != (offset & (0L-BLOCKLEN))) /* same entry as last ref? */
  409.       findload_cache (offset & (0L-BLOCKLEN));
  410.  
  411.    far_memcpy(dest, (void far *) &cur_cache->pixel[col_subscr], size);
  412.    cur_cache->dirty = 0;
  413.    return 1;
  414. }
  415.  
  416.  
  417. void targa_readdisk(unsigned int col, unsigned int row,
  418.             BYTE *red, BYTE *green, BYTE *blue)
  419. {
  420.    col *= 3;
  421.    *blue  = (BYTE)readdisk(col,row);
  422.    *green = (BYTE)readdisk(++col,row);
  423.    *red   = (BYTE)readdisk(col+1,row);
  424. }
  425.  
  426. void writedisk(unsigned int col, unsigned int row, unsigned int color)
  427. {
  428.    int col_subscr;
  429.    long offset;
  430.    char buf[41];
  431.    if (--timetodisplay < 0) {  /* time to display status? */
  432.       if (dotmode == 11) {
  433.      sprintf(buf," writing line %4d",
  434.         (row >= (unsigned int)sydots) ? row-sydots : row); /* adjust when potfile */
  435.      dvid_status(0,buf);
  436.      }
  437.       timetodisplay = 1000;
  438.       }
  439.    if (row != (unsigned int)cur_row)    { /* try to avoid ghastly code generated for multiply */
  440.       if (row >= colsize) /* while we're at it avoid this test if not needed  */
  441.      return;
  442.       cur_row_base = (long)(cur_row = row) * rowsize;
  443.       }
  444.    if (col >= rowsize)
  445.       return;
  446.    offset = cur_row_base + col;
  447.    col_subscr = (short)offset & (BLOCKLEN-1);
  448.    if (cur_offset != (offset & (0L-BLOCKLEN))) /* same entry as last ref? */
  449.       findload_cache(offset & (0L-BLOCKLEN));
  450.    if (cur_cache->pixel[col_subscr] != (color & 0xff)) {
  451.       cur_cache->pixel[col_subscr] = (BYTE)color;
  452.       cur_cache->dirty = 1;
  453.       }
  454. }
  455.  
  456. int ToMemDisk(long offset, int size, void far *src)
  457. {
  458.    int col_subscr =  (int)(offset & (BLOCKLEN - 1));
  459.  
  460.    if (col_subscr + size > BLOCKLEN)        /* access violates  a */
  461.       return 0;                                 /*   cache boundary   */
  462.  
  463.    if (cur_offset != (offset & (0L-BLOCKLEN))) /* same entry as last ref? */
  464.       findload_cache (offset & (0L-BLOCKLEN));
  465.  
  466.    far_memcpy((void far *) &cur_cache->pixel[col_subscr], src, size);
  467.    cur_cache->dirty = 1;
  468.    return 1;
  469. }
  470.  
  471. void targa_writedisk(unsigned int col, unsigned int row,
  472.             BYTE red, BYTE green, BYTE blue)
  473. {
  474.    writedisk(col*=3,row,blue);
  475.    writedisk(++col, row,green);
  476.    writedisk(col+1, row,red);
  477. }
  478.  
  479. static void _fastcall near findload_cache(long offset) /* used by read/write */
  480. {
  481. #ifndef XFRACT
  482.    unsigned int tbloffset;
  483.    int i,j;
  484.    unsigned int far *fwd_link;
  485.    BYTE far *pixelptr;
  486.    BYTE tmpchar;
  487.    cur_offset = offset; /* note this for next reference */
  488.    /* check if required entry is in cache - lookup by hash */
  489.    tbloffset = hash_ptr[ ((unsigned short)offset >> BLOCKSHIFT) & (HASHSIZE-1) ];
  490.    while (tbloffset != 0xffff) { /* follow the hash chain */
  491.       cur_cache = (struct cache far *)((char far *)cache_start + tbloffset);
  492.       if (cur_cache->offset == offset) { /* great, it is in the cache */
  493.      cur_cache->lru = 1;
  494.      return;
  495.      }
  496.       tbloffset = cur_cache->hashlink;
  497.       }
  498.    /* must load the cache entry from backing store */
  499.    for(;;) { /* look around for something not recently used */
  500.       if (++cache_lru >= cache_end)
  501.      cache_lru = cache_start;
  502.       if (cache_lru->lru == 0)
  503.      break;
  504.       cache_lru->lru = 0;
  505.       }
  506.    if (cache_lru->dirty) /* must write this block before reusing it */
  507.       write_cache_lru();
  508.    /* remove block at cache_lru from its hash chain */
  509.    fwd_link = hash_ptr
  510.         + (((unsigned short)cache_lru->offset >> BLOCKSHIFT) & (HASHSIZE-1));
  511.    tbloffset = (char far *)cache_lru - (char far *)cache_start;
  512.    while (*fwd_link != tbloffset)
  513.       fwd_link = &((struct cache far *)((char far *)cache_start+*fwd_link))->hashlink;
  514.    *fwd_link = cache_lru->hashlink;
  515.    /* load block */
  516.    cache_lru->dirty  = 0;
  517.    cache_lru->lru    = 1;
  518.    cache_lru->offset = offset;
  519.    pixelptr = &cache_lru->pixel[0];
  520.    if (offset > high_offset) { /* never been this high before, just clear it */
  521.       high_offset = offset;
  522.       for (i = 0; i < BLOCKLEN; ++i)
  523.      *(pixelptr++) = 0;
  524.       }
  525.    else {
  526.       if (offset != seek_offset)
  527.      (*doseek)(offset >> pixelshift);
  528.       seek_offset = offset + BLOCKLEN;
  529.       switch (pixelshift) {
  530.      case 0:
  531.         for (i = 0; i < BLOCKLEN; ++i)
  532.            *(pixelptr++) = (*get_char)();
  533.         break;
  534.      case 1:
  535.         for (i = 0; i < BLOCKLEN/2; ++i) {
  536.            tmpchar = (*get_char)();
  537.            *(pixelptr++) = (BYTE)(tmpchar >> 4);
  538.            *(pixelptr++) = (BYTE)(tmpchar & 15);
  539.            }
  540.         break;
  541.      case 2:
  542.         for (i = 0; i < BLOCKLEN/4; ++i) {
  543.            tmpchar = (*get_char)();
  544.            for (j = 6; j >= 0; j -= 2)
  545.           *(pixelptr++) = (BYTE)((tmpchar >> j) & 3);
  546.            }
  547.         break;
  548.      case 3:
  549.         for (i = 0; i < BLOCKLEN/8; ++i) {
  550.            tmpchar = (*get_char)();
  551.            for (j = 7; j >= 0; --j)
  552.           *(pixelptr++) = (BYTE)((tmpchar >> j) & 1);
  553.            }
  554.         break;
  555.      }
  556.       }
  557.    /* add new block to its hash chain */
  558.    fwd_link = hash_ptr + (((unsigned short)offset >> BLOCKSHIFT) & (HASHSIZE-1));
  559.    cache_lru->hashlink = *fwd_link;
  560.    *fwd_link = (char far *)cache_lru - (char far *)cache_start;
  561.    cur_cache = cache_lru;
  562. #endif
  563.    }
  564.  
  565. static struct cache far * _fastcall near find_cache(long offset)
  566. /* lookup for write_cache_lru */
  567. {
  568. #ifndef XFRACT
  569.    unsigned int tbloffset;
  570.    struct cache far *ptr1;
  571.    tbloffset = hash_ptr[ ((unsigned short)offset >> BLOCKSHIFT) & (HASHSIZE-1) ];
  572.    while (tbloffset != 0xffff) {
  573.       ptr1 = (struct cache far *)((char far *)cache_start + tbloffset);
  574.       if (ptr1->offset == offset)
  575.      return (ptr1);
  576.       tbloffset = ptr1->hashlink;
  577.       }
  578.    return (NULL);
  579. #endif
  580. }
  581.  
  582. static void near write_cache_lru()
  583. {
  584.    int i,j;
  585.    BYTE far *pixelptr;
  586.    long offset;
  587.    BYTE tmpchar = 0; 
  588.    struct cache far *ptr1, far *ptr2;
  589. #define WRITEGAP 4 /* 1 for no gaps */
  590.    /* scan back to also write any preceding dirty blocks, skipping small gaps */
  591.    ptr1 = cache_lru;
  592.    offset = ptr1->offset;
  593.    i = 0;
  594.    while (++i <= WRITEGAP) {
  595.       if ((ptr2 = find_cache(offset -= BLOCKLEN)) != NULL && ptr2->dirty) {
  596.      ptr1 = ptr2;
  597.      i = 0;
  598.      }
  599.       }
  600.    /* write all consecutive dirty blocks (often whole cache in 1pass modes) */
  601.    /* keep going past small gaps */
  602. write_seek:
  603.    (*doseek)(ptr1->offset >> pixelshift);
  604. write_stuff:
  605.    pixelptr = &ptr1->pixel[0];
  606.    switch (pixelshift) {
  607.       case 0:
  608.      for (i = 0; i < BLOCKLEN; ++i)
  609.         (*put_char)(*(pixelptr++));
  610.      break;
  611.       case 1:
  612.      for (i = 0; i < BLOCKLEN/2; ++i) {
  613.         tmpchar = (BYTE)(*(pixelptr++) << 4);
  614.         tmpchar = (BYTE)(tmpchar + *(pixelptr++));
  615.         (*put_char)(tmpchar);
  616.         }
  617.      break;
  618.       case 2:
  619.      for (i = 0; i < BLOCKLEN/4; ++i) {
  620.         for (j = 6; j >= 0; j -= 2)
  621.            tmpchar = (BYTE)((tmpchar << 2) + *(pixelptr++));
  622.         (*put_char)(tmpchar);
  623.         }
  624.      break;
  625.       case 3:
  626.      for (i = 0; i < BLOCKLEN/8; ++i) {
  627.         (*put_char)((BYTE)
  628.             ((((((((((((((*pixelptr
  629.                         << 1)
  630.                         | *(pixelptr+1) )
  631.                         << 1)
  632.             | *(pixelptr+2) )
  633.                         << 1)
  634.                         | *(pixelptr+3) )
  635.                         << 1)
  636.             | *(pixelptr+4) )
  637.                         << 1)
  638.                         | *(pixelptr+5) )
  639.                         << 1)
  640.             | *(pixelptr+6) )
  641.                         << 1)
  642.                         | *(pixelptr+7)));
  643.         pixelptr += 8;
  644.         }
  645.      break;
  646.       }
  647.    ptr1->dirty = 0;
  648.    offset = ptr1->offset + BLOCKLEN;
  649.    if ((ptr1 = find_cache(offset)) != NULL && ptr1->dirty != 0)
  650.       goto write_stuff;
  651.    i = 1;
  652.    while (++i <= WRITEGAP) {
  653.       if ((ptr1 = find_cache(offset += BLOCKLEN)) != NULL && ptr1->dirty != 0)
  654.      goto write_seek;
  655.       }
  656.    seek_offset = -1; /* force a seek before next read */
  657. }
  658.  
  659. /* Seek, get_char, put_char routines follow for expanded, extended, disk.
  660.    Note that the calling logic always separates get_char and put_char
  661.    sequences with a seek between them.    A get_char is never followed by
  662.    a put_char nor v.v. without a seek between them.
  663.    */
  664.  
  665. static void _fastcall near disk_seek(long offset)    /* real disk seek */
  666. {
  667.    fseek(fp,offset+headerlength,SEEK_SET);
  668.    }
  669.  
  670. static BYTE near disk_getc()            /* real disk get_char */
  671. {
  672.    return((BYTE)getc(fp));
  673.    }
  674.  
  675. static void _fastcall near disk_putc(BYTE c)    /* real disk put_char */
  676. {
  677.    putc(c,fp);
  678.    }
  679.  
  680. static void _fastcall near exp_seek(long offset)    /* expanded mem seek */
  681. {
  682.    int page;
  683.    expoffset = (short)offset & 0x3fff;
  684.    page = (int)(offset >> 14);
  685.    if (page != oldexppage) { /* time to get a new page? */
  686.       oldexppage = page;
  687.       emmgetpage(page,emmhandle);
  688.       }
  689.    }
  690.  
  691. static BYTE near exp_getc()            /* expanded get_char */
  692. {
  693.    if (expoffset > 0x3fff) /* wrapped into a new page? */
  694.       exp_seek((long)(oldexppage+1) << 14);
  695.    return(expmemoryvideo[expoffset++]);
  696.    }
  697.  
  698. static void _fastcall near exp_putc(BYTE c)    /* expanded put_char */
  699. {
  700.    if (expoffset > 0x3fff) /* wrapped into a new page? */
  701.       exp_seek((long)(oldexppage+1) << 14);
  702.    expmemoryvideo[expoffset++] = c;
  703.    }
  704.  
  705. static void near ext_writebuf(void) /* subrtn for extended mem seek/put_char */
  706. {
  707. #ifndef XFRACT
  708.    struct XMM_Move MoveStruct;
  709.    MoveStruct.Length = extbufptr - charbuf;
  710.    MoveStruct.SourceHandle = 0; /* Source is conventional memory */
  711.    MoveStruct.SourceOffset = (unsigned long)charbuf;
  712.    MoveStruct.DestHandle = xmmhandle;
  713.    MoveStruct.DestOffset = extoffset;
  714.    xmmmoveextended(&MoveStruct);
  715. #endif
  716.    }
  717.  
  718. static void _fastcall near ext_seek(long offset)    /* extended mem seek */
  719. {
  720.    if (extbufptr > endreadbuf) /* only true if there was a put_char sequence */
  721.       ext_writebuf();
  722.    extoffset = offset;
  723.    extbufptr = endreadbuf = charbuf;
  724.    }
  725.  
  726. static BYTE near ext_getc()            /* extended get_char */
  727. {
  728. #ifndef XFRACT
  729.    struct XMM_Move MoveStruct;
  730.    if (extbufptr >= endreadbuf) { /* drained the last read buffer we fetched? */
  731.       MoveStruct.Length = XMMREADLEN;
  732.       MoveStruct.SourceHandle = xmmhandle; /* Source is extended memory */
  733.       MoveStruct.SourceOffset = extoffset;
  734.       MoveStruct.DestHandle = 0;
  735.       MoveStruct.DestOffset = (unsigned long)charbuf;
  736.       xmmmoveextended(&MoveStruct);
  737.       extoffset += XMMREADLEN;
  738.       endreadbuf = XMMREADLEN + (extbufptr = charbuf);
  739.       }
  740.    return (*(extbufptr++));
  741. #endif
  742.    }
  743.  
  744. static void _fastcall near ext_putc(BYTE c)    /* extended get_char */
  745. {
  746.    if (extbufptr >= endwritebuf) { /* filled the local write buffer? */
  747.       ext_writebuf();
  748.       extoffset += XMMWRITELEN;
  749.       extbufptr = charbuf;
  750.       }
  751.    *(extbufptr++) = c;
  752.    }
  753.  
  754. void dvid_status(int line,char far *msg)
  755. {
  756.    char buf[41];
  757.    int attrib;
  758.    memset(buf,' ',40);
  759.    far_memcpy(buf,msg,far_strlen(msg));
  760.    buf[40] = 0;
  761.    attrib = C_DVID_HI;
  762.    if (line >= 100) {
  763.       line -= 100;
  764.       attrib = C_STOP_ERR;
  765.       }
  766.    putstring(BOXROW+8+line,BOXCOL+12,attrib,buf);
  767.    movecursor(25,80);
  768. }
  769.  
  770.